package com.wang.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.Result;
import com.wang.entity.Meeting;
import com.wang.mapper.MeetingMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/meeting")
public class MeetingController {
    @Resource
    MeetingMapper MeetingMapper;




    @PostMapping
    public Result<?> save(@RequestBody Meeting Meeting){
        MeetingMapper.insert(Meeting);
        return Result.success();

    }


    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue ="1" ) Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Meeting> wrapper = Wrappers.<Meeting>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Meeting::getName, search);
        }
        Page<Meeting> MeetingPage=MeetingMapper.selectPage(new Page<>(pageNum,pageSize),wrapper );
        return Result.success(MeetingPage);

    }

    @PutMapping
    public Result<?> update(@RequestBody Meeting Meeting){
       MeetingMapper.updateById(Meeting);
        return Result.success();

    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id){
        return Result.success(MeetingMapper.selectById(id));
    }


    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        MeetingMapper.deleteById(id);
        return Result.success();

    }

}
